<template>
	<view>
		<view class="inv-top-menu-bar">
			<view class="top-menu">
				<view class="top-menu-list">
					<text class="t-btn" :class="params.type == 1 ? 'active' : ''" @click="changeTab(1)">
						获得记录
					</text>
					<text class="t-btn" :class="params.type == 2 ? 'active' : ''" @click="changeTab(2)">
						消耗记录
					</text>
				</view>
			</view>
		</view>
		<view class="form_split_10"></view>
		<u-empty text="没有找到对应的数据" v-if="show_empty === true" />
		<view class="box_1">
			<view class="item" v-for="(item, index) in dataset" :key="index">
				<view class="tx1">
					{{ item.content }}
					<view class="right_txt" :class="item.op == 2 ? 'con' : ''">
						{{ item.op == 1 ? "+" : "-" }}{{ item.points }}
					</view>
				</view>
				<view class="tx2">{{ item.addtime | timeFilter }}</view>
			</view>
		</view>
		<u-loadmore class="u-loadmore" v-if="dataset.length >= params.pagesize" :status="finished_status" />
	</view>
</template>

<script>
	import {
		parseTime
	} from '@/utils/index'
	import http from '@/utils/http'
	import api from '@/api'
	export default {
		name: 'PointsLog',
		filters: { 
			timeFilter(timestamp) {
				return parseTime(timestamp, '{y}-{m}-{d} {h}:{i}')
			}
		},
		data() {
			return {
				dataset: [],
				finished_status:'loadmore',
				show_empty: false,
				params: {
					type: 1,
					page: 1,
					pagesize: 10
				}
			}
		},
		mounted() {
			this.fetchData(true)
		},
		methods: {
			fetchData(init) {
				if (init === true) {
					this.params.page = 1
				} else{
					this.finished_status = 'loading'
				}
				http
					.get(api.member_pointslog, this.params)
					.then(res => {
						if (init === true) {
							this.show_empty = false
							this.dataset = [...res.data.data.items]
						} else {
							this.dataset = this.dataset.concat(res.data.data.items)
						}
						// 数据全部加载完成
						if (res.data.data.items.length < this.params.pagesize) {
							this.finished_status = 'nomore'
							if (init === false) {
								this.finished_status = 'nomore'
							} else if (res.data.data.items.length == 0) {
								this.show_empty = true
							}
						}
					})
					.catch(() => {})
			},
			changeTab(type) {
				if (type == this.params.type) {
					return false
				}
				this.params.type = type
				this.fetchData(true)
			},
			onload(){
				this.params.page++
				this.fetchData(false)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.box_1 {
		.item {
			.tx2 {
				font-size: 25rpx;
				color: #999999;
			}

			.tx1 {
				.right_txt {
					position: absolute;
					right: 0;
					top: 50%;
					transform: translate(0, -50%);
					font-size: 28rpx;
					color: #009900;
					font-weight: normal;

					&.con {
						color: #ff5d24;
					}
				}

				position: relative;
				padding: 0 100rpx 0 0;
				font-size: 32rpx;
				font-weight: bold;
				color: #333333;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
				margin-bottom: 16rpx;
			}

			width: 100%;
			padding:32rpx 0 26rpx 0;

			&:not(:last-child) {
				border-bottom: 2rpx solid #f7f7f7;
			}
		}

		width: 100%;
		padding: 0 30rpx;
	}

	.inv-top-menu-bar {
		background: #ffffff;

		.top-menu {
			overflow: hidden;
			overflow-x: scroll;
			-webkit-overflow-scrolling: touch;

			&::-webkit-scrollbar {
				display: none;
			}
		}

		.top-menu-list {
			white-space: nowrap;

			.t-btn {
				position: relative;
				white-space: nowrap;
				display: inline-block;
				text-align: center;
				color: #666666;
				font-size: 30rpx;
				text-decoration: none;
				padding: 30rpx 0;
				width: 50%;
				-webkit-tap-highlight-color: rgba(0, 0, 0, 0.3);

				&::after {
					position: absolute;
					left: 50%;
					bottom: 4rpx;
					width: 32rpx;
					height: 6rpx;
					background-color: #1787fb;
					border-radius: 100%;
					transform: translate(-50%, 0);
				}

				&.active {
					color: #1787fb;

					&::after {
						content: " ";
					}
				}
			}
		}
	}
</style>
